package com.romaremez.testuser.src.testsystem;

import com.romaremez.testuser.src.questionanswer.Question;
import com.romaremez.testuser.src.userdata.UserSession;
import com.romaremez.testuser.src.datautils.DataLoader;

import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * Класс управляет действиями юзера
 */
public class TestUser {
    private UserSession userSession;
    private List<Question> questions;

    public TestUser(UserSession userSession) {
        this.userSession = userSession;
        getQuestions();
    }

    /**
     * Method for read and shuffle questions.
     */
    private void getQuestions() {
        questions = new DataLoader().readQuestionList();
        Collections.shuffle(questions);
    }

    public void startTest() {
        askQuestions();
        if (isTestComplete()) {
            endTest();
            userSession.finishSession();
        }
    }

    private boolean isTestComplete() {
        return userSession.size() >= questions.size();
    }

    /**
     * Method looks for next questionStep.
     */
    private QuestionStep getNextQuestionStep() {
        QuestionStep result;
        for (Question question : questions) {
            result = new QuestionStep(new Question(question));
            if (!userSession.contains(result)) {
                userSession.addQuestionStep(result);
                return result;
            }
        }
        return null;
    }

    private QuestionStep getPreviousStep() {
        int previousIndex = userSession.size() - 1;

        if ( previousIndex < 0) {
            return getNextQuestionStep();
        }
       return userSession.getQuestionStep(previousIndex);
    }

    private void deleteCurrentQuestionStep(QuestionStep questionStep) {
        userSession.deleteQuestionStep(questionStep);
    }

    private void askQuestions() {
        QuestionStep currentStep = getNextQuestionStep();
        while (true) {
            if (currentStep == null) {
                break;
            }
            currentStep.showQuestion();
            String userInput = getAnswer();
            switch (userInput) {
                case "<undo>":
                            deleteCurrentQuestionStep(currentStep);
                            currentStep = getPreviousStep();
                            break;

                case "<exit>":
                            deleteCurrentQuestionStep(currentStep);
                            return;
                default:
                            currentStep.saveAnswer(userInput);
                            currentStep = getNextQuestionStep();
            }
        }
    }

    /**
     * Method reads the answer from console.
     */
    private String getAnswer() {
        Scanner in = new Scanner(System.in);
        System.out.println("Введите ответ:");
        return in.nextLine();
    }

    /**
     * Method which ends the test by printing the user's score.
     */
    private void endTest() {
        System.out.printf("Тест окончен%nВаш счет: %d%n", userSession.getPoints());
    }
}



